home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-09-09 | 5.7 KB | 205 lines | [TEXT/CWIE] |
- // Useful utilities for working with pictures
- //
- // David Hayward
- // Developer Technical Support
- // AppleLink: DEVSUPPORT
- //
- // Copyrite 1995, Apple Computer,Inc
- //
- // Routines for reading and writing pictures
- // and for drawing pictures using bottlenecks
- //
- // 12/13/94 david first cut
- // 9/19/95 david improved comments
-
-
- #include "qdUtils.h"
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- DoReadPICT
- *------------------------------------------------------------------------------*
- This reads a picture file from a FSSpec into a PicHandle
- \*------------------------------------------------------------------------------*/
- OSErr DoReadPICT ( FSSpec spec, PicHandle *pict )
- {
- long fileSize ;
- short ref ;
- OSErr err ;
-
- // try to open the file
- err = FSpOpenDF( &spec, fsRdPerm, &ref ) ;
- if ( err ) return nil;
-
- // pict files have a 512 byte header at the front - we dont care about this
- // we can find the size of the pict by subtracting 512 bytes from the length
- // of the file. We then want to size a handle to that and read the data
- // into the handle.
-
- err = GetEOF( ref, &fileSize ) ;
- if ( err ) goto bail;
-
- err = SetFPos( ref, fsFromStart, 512) ;
- if ( err ) goto bail;
-
- fileSize -= 512 ;
-
- *pict = (PicHandle)NewHandle( fileSize ) ;
- if( *pict == nil )
- {
- err = MemError() ;
- goto bail;
- }
-
- HLock( (Handle)*pict ) ;
- err = FSRead( ref, &fileSize, (Ptr)**pict ) ;
- HUnlock( (Handle)*pict ) ;
- if( err )
- {
- DisposeHandle( (Handle)*pict ) ;
- *pict = nil ;
- }
-
- bail:
- FSClose( ref ) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoWritePICT
- *------------------------------------------------------------------------------*
- This writes a picture file to a FSSpec from a PicHandle
- If the file referenced by the FSSpec already exists,
- then it is deleted and a new file is created.
- \*------------------------------------------------------------------------------*/
- OSErr DoWritePICT ( FSSpec spec, PicHandle pict, OSType creator )
- {
- short refNum, count;
- long inOutCount;
- unsigned char header[512];
- OSErr err ;
-
- for ( count=0; count<512; count++ )
- header[count] = 0x00;
-
- err = FSpCreate( &spec, creator, 'PICT', 0 /*reply.sfScript*/ ) ;
-
- /* this is quick 'n' dirty or there'd be more robust handling here */
- if (err == dupFNErr)
- {
- err = FSpDelete( &spec ) ;
- err = FSpCreate( &spec, creator, 'PICT', 0 /*reply.sfScript*/ ) ;
- }
-
- err = FSpOpenDF( &spec, fsWrPerm, &refNum) ;
-
- if ( err == noErr )
- {
- inOutCount = 512;
- err = FSWrite( refNum, &inOutCount, header) ; /* write the header */
-
- if (err == noErr) /* don't write if error */
- {
- inOutCount = GetHandleSize( (Handle)pict ) ;
- HLock((Handle)pict) ;
- err = FSWrite( refNum, &inOutCount, *pict ) ;
- HUnlock( (Handle)pict ) ;
- }
- FSClose( refNum ) ; /* close it */
- }
-
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- NewSmallGWorld
- *------------------------------------------------------------------------------*
- This creates a new offscreen GWorld that is only 2pixels x 2pixels x 1bit
- This is sufficient if all you need to do is draw into the offscreen
- using passive QuickDraw bottleneck procs.
- \*------------------------------------------------------------------------------*/
- QDErr NewSmallGWorld ( GWorldPtr *offscreen )
- {
- Rect rect = { 0, 0, 2, 2 } ;
- return NewGWorld( offscreen, 1, &rect, nil, nil, nil ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- NewGWorldClear
- *------------------------------------------------------------------------------*
- This creates a new offscreen GWorld using the same arguments as NewGWorld()
- The only difference is that this routine erases the offscreen to white
- after creating it.
- \*------------------------------------------------------------------------------*/
- QDErr NewGWorldClear ( GWorldPtr *offscreen, short depth, const Rect *rect,
- CTabHandle cTable, GDHandle aGDevice, GWorldFlags flags )
- {
- QDErr err ;
- CGrafPtr savePort;
- GDHandle saveGDev;
-
- err = NewGWorld( offscreen, depth, rect, cTable, aGDevice, flags ) ;
- if (err) return err ;
-
- GetGWorld( &savePort, &saveGDev ) ;
- SetGWorld( *offscreen, nil ) ;
- EraseRect( rect ) ;
- SetGWorld( savePort, saveGDev ) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DrawPicHandleUsingBottlenecks
- *------------------------------------------------------------------------------*
- This creates draws a pictute into CGrafPort using the bottleneck supplied
- in the CQDProcs parameter. If the port parameter is nil, then this
- routine temporarily creates a small offscreen GWorld to draw into.
- \*------------------------------------------------------------------------------*/
- OSErr DrawPicHandleUsingBottlenecks ( PicHandle pict, CQDProcs procs, CGrafPtr port )
- {
- OSErr err = noErr ;
- CGrafPtr savePort;
- GDHandle saveGDev;
- GWorldPtr offscreen;
- Rect pictRect;
- PixMapHandle pixmap;
-
- if ( port==nil )
- {
- err = NewSmallGWorld( &offscreen ) ;
- if (err) return err ;
- }
- else
- offscreen = port ;
-
-
- offscreen->grafProcs = &procs ; // must be a color port
-
- GetGWorld( &savePort, &saveGDev ) ;
- SetGWorld( offscreen, nil ) ;
-
- pictRect = (**pict).picFrame ;
-
- pixmap = GetGWorldPixMap(offscreen);
- if(LockPixels(pixmap))
- DrawPicture( pict, &pictRect ) ;
- UnlockPixels(pixmap);
-
- SetGWorld( savePort, saveGDev ) ;
-
- if ( port==nil )
- DisposeGWorld( offscreen ) ;
-
- return err ;
- }